package xjtu.sad.zbsj.task.annotater.manageTask;

import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.*;
import xjtu.sad.zbsj.common.PageParam;
import xjtu.sad.zbsj.common.Result;
import xjtu.sad.zbsj.dao.manageTask.ManageTaskDao;

import javax.annotation.Resource;


@RestController
@RequestMapping("/task/annotater/manageTask")
public class ManageTaskAPI {
    @Resource
    private ManageTaskService manageTaskService;

    @PostMapping("/TaskList")
    public Result getTaskList(@RequestBody PageParam pageParam) {
        System.out.println(pageParam);
        Page<ManageTask> manageTaskPage = manageTaskService.getTaskList(pageParam);
        String TaskListSum = manageTaskDao.getTaskListPageSum();
        return Result.success(TaskListSum, manageTaskPage);
    }


    @PostMapping("/MyTask")
    public Result getMyTask(@RequestParam Integer userId, @RequestBody PageParam pageParam) {
        String MyTaskSum = manageTaskDao.getMyTaskPageSum(userId);
        return Result.success(MyTaskSum, manageTaskService.getMyTask(userId, pageParam));
    }

    @PostMapping("/markDetail")
    public Result getMarkDetail(@RequestParam Integer draw_id, @RequestBody PageParam pageParam) {
        String MarkSum = manageTaskDao.getMarkPageSum(draw_id);
        return Result.success(MarkSum, manageTaskService.getMarkDetail(draw_id, pageParam));
    }

    @Resource
    private ManageTaskDao manageTaskDao;

    @RequestMapping("/drawTask")
    public Result TaskDetail(@RequestBody DrawTask drawTask) {
        Integer task_status = manageTaskDao.getTaskStatus(drawTask.getTask_id());
        Integer userId = drawTask.getUser_id();

        if (task_status == 1) {
            boolean allowDraw = manageTaskService.judgeAllowDraw(userId, drawTask.getTask_id());
            if (allowDraw) {
                String message = manageTaskService.drawTask(drawTask, userId);
                if (message == "领取成功") {
                    return Result.success(message);
                } else {
                    return Result.fail(Result.ERR_CODE_BUSINESS, message);
                }
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "请完成该任务所有标注后再次领取");
            }
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "任务已领取结束");
        }
    }

    @GetMapping("/getUnPassReason")
    public Result getUnPassReason(@RequestParam Integer markId) {
        String reason = manageTaskDao.getUnPassReason(markId);
        return Result.success(reason);
    }
}




